window.openLayer = function (obj) {
  if (obj.parent) {
    parent.window.layerData = {};
  } else {
    window.layerData = {};
  }

  if (obj.content) {
    obj.type = 2;
  }
  if (obj.area === undefined) {
    obj.area = ["1200px", "650px"];
  }
  if (obj.shadeClose === undefined) {
    obj.shadeClose = false;
  }
  // if (obj.offset === undefined) {
  //   obj.offset = "15px";
  // }
  if (obj.shade === undefined) {
    obj.shade = 0.1;
  }
  // if (obj.fixed === undefined) {
  //   obj.fixed = false;
  // }
  // if (obj.resize === undefined) {
  //   obj.resize = true;
  // }
  if (obj.skin === undefined) {
    obj.skin = "layui-layer-admin";
  }
  if (obj.title === undefined) {
    obj.title = "操作";
  }
  if (obj.maxmin === undefined) {
    obj.maxmin = false;
  }
  var endFn = obj.end;
  obj.end = function () {
    if (obj.parent) {
      parent.layer.closeAll("tips");
    } else {
      layer.closeAll("tips");
    }
    endFn && endFn();
  };
  var successFn = obj.success;
  obj.success = function (s, t) {
    successFn && successFn(s, t);
  };
  var index;
  if (obj.parent) {
    index = parent.layer.open(obj);
  } else {
    index = layer.open(obj);
  }

  if (obj.data) {
    if (obj.parent) {
      parent.window.layerData["d" + index] = obj.data;
    } else {
      window.layerData["d" + index] = obj.data;
    }
  }
  return index;
};

window.getLayerData = function (p, i) {
  if (i === undefined) {
    i = parent.layer.getFrameIndex(window.name);
    if (i === undefined) {
      return null;
    } else {
      return parent.window.getLayerData(p, parseInt(i));
    }
  } else {
    if (isNaN(i)) {
      i = window.getLayerIndex(i);
    }
  }
  if (i === undefined) {
    return;
  }
  var q = window.layerData["d" + i];
  if (p && q) {
    return q[p];
  }
  return q;
};

window.getLayerIndex = function (i) {
  if (!i) {
    return parent.layer.getFrameIndex(window.name);
  }
  var p = $(i).parents(".layui-layer").first().attr("id");
  if (p && p.length >= 11) {
    return p.substring(11);
  }
};

window.closeLayer = function (f, i) {
  if (typeof f === "function") {
    f && f();
  } else {
    i = f;
  }
  if (i) {
    layer.close(getLayerIndex(i));
  } else {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
  }
};

const WBH_YES = function (index, fnc) {
  layer.close(index);
  fnc && fnc();
};

const WBH_LAYER_OPTIONS = function (options, yes, cancel) {
  if (typeof options === "function") {
    cancel = yes;
    yes = options;
    options = {};
  } else if (options === undefined) {
    options = {};
  }
  if (options.skin === undefined) {
    options.skin = "layui-layer-admin";
  }
  if (options.shade === undefined) {
    options.shade = 0.1;
  }
  if (options.title === undefined) {
    options.title = "系统提示";
  }
  options.closeBtn = 0;
  return {
    options: options,
    yes: yes,
    cancel: cancel,
  };
};

window.$baseMsg = function (content, options, end) {
  const data = WBH_LAYER_OPTIONS(options, end);
  data.options.title = undefined;
  data.options.skin = undefined;
  data.options.shade = undefined;
  return layer.msg(content, data.options, data.end);
};

window.$baseAlert = function (content, options, yes) {
  const data = WBH_LAYER_OPTIONS(options, yes);
  return layer.alert(content, data.options, (index) => WBH_YES(index, data.yes));
};

window.$baseConfirm = function (content, options, yes, cancel) {
  const data = WBH_LAYER_OPTIONS(options, yes, cancel);
  options.skin = undefined;
  return layer.confirm(
    content,
    data.options,
    (index) => WBH_YES(index, data.yes),
    (index) => WBH_YES(index, data.cancel)
  );
};

// 关闭
const useCloseBtn = function () {
  const index = parent.layer.getFrameIndex(window.name);
  const closeBtn = Vue.ref({
    t: "关闭",
    i: "layui-icon-close",
    index: index,
    close: () => {
      if (index) {
        parent.layer.close(index);
      } else {
        history.back();
      }
    },
  });
  if (!index) (closeBtn.value.t = "返回"), (closeBtn.value.i = "layui-icon-return");
  return closeBtn;
};
